<?php

namespace app\api\controller;

use app\common\controller\Api;

class Good extends Api
{
    protected $noNeedLogin = ['*'];
    //获取商品分类
    public function gettypelist()
    {
        $page = input('page',1);
        $limit = input('limit',8);
        $list = db('goodtype')
            ->field('id,name,image')
            ->where([
                'deletetime'=>null
            ])
            ->order('weigh DESC')
            ->page($page,$limit)
            ->select();
        $this->success('ok',$list);
    }

    //获取推荐商品列表
    public function gettuigoods()
    {
        $page = input('page',1);
        $limit = input('limit',10);
        $search = input('search');
        $where = [];
        $where['deletetime'] = null;
        $where['tui_switch'] = 1;
        if ($search) $where['name'] = ['like','%'.$search.'%'];
        $list = db('products')
            ->field('id,name,image,price,del_price,vip_price,sellnum')
            ->where($where)
            ->order('weigh DESC')
            ->page($page,$limit)
            ->select();
        $this->success('ok',$list);
    }

    //获取分类商品列表
    public function gettypegoods()
    {
        $page = input('page',1);
        $limit = input('limit',10);
        $search = input('search');
        $goodtype_id = input('goodtype_id');
        $where = [];
        $where['deletetime'] = null;
        if ($goodtype_id) $where['goodtype_id'] = $goodtype_id;
        if ($search) $where['name'] = ['like','%'.$search.'%'];
        $list = db('products')
            ->field('id,name,image,price,del_price,vip_price,sellnum')
            ->where($where)
            ->order('weigh DESC')
            ->page($page,$limit)
            ->select();
        $this->success('ok',$list);
    }

    //获取商品详情
    public function getgooddetail()
    {
        $good_id = input('good_id');
        $good = db('products')
            ->find($good_id);
        if (!$good){
            $this->error('商品不存在');
        }
        $good['labeljson'] = json_decode($good['labeljson'],true);
        $skus = db('productssku')
            ->where('products_id',$good_id)
            ->where('deletetime',null)
            ->select();
        $truesku = [];
        $stock = 0;
        $default_skukey = '';
        foreach ($skus as &$sku){
            $stock += $sku['stock'];
            $sku['specificationvalues_ids'] = explode(',',$sku['specificationvalues_ids']);
            sort($sku['specificationvalues_ids']);
            $truekey = implode('_',$sku['specificationvalues_ids']);
            if (!$default_skukey){
                $default_skukey = $truekey;
            }
            $truesku[$truekey] = $sku;
        }
        unset($sku);
        $default_skukey_ids = explode('_',$default_skukey);
        $skutypes = db('specifications')
            ->where('products_id',$good_id)
            ->where('deletetime',null)
            ->select();
        $skulist = [];
        $secletskuids = [];
        foreach ($skutypes as $skutype){
            $skulistitem = db('specificationvalues')
                ->where('specifications_id',$skutype['id'])
                ->where('deletetime',null)
                ->field('id,name')
                ->select();
            foreach ($skulistitem as &$item){
                if(in_array($item['id'],$default_skukey_ids)){
                    $item['isselect'] = true;
                    $secletskuids[] = $item['id'];
                }else{
                    $item['isselect'] = false;
                }
            }
            $skulist[] = [
                'name'=>  $skutype['name'],
                'list'=>$skulistitem
            ];
        }
        $good['secletskuids'] = $secletskuids;
        $good['default_skugood'] = $truesku[$default_skukey];
        $good['stock'] = $stock;
        $good['skulist'] = $skulist;
        $good['skugoods'] = $truesku;
        $good['images'] = explode(',',$good['images']);
        $this->success('ok',$good);
    }

}